package io.renren.common.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @className: WebConfig
 * @description: web配置
 * @author: 熊少杰
 * @create: 2021-08-23 23:49
 **/
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${web.virtual-path}")
    private String pathPattern;

    @Value("${web.upload-path}")
    private String resourcePath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 使用一个Controller统一接管图片地址映射任务,所以这里进行注释了（io.renren.modules.file.controller.FileOptionController）
//        registry.addResourceHandler(pathPattern + "**").addResourceLocations("file:" + resourcePath);
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler(pathPattern+"/**").addResourceLocations("file:"+resourcePath);
    }

    /**
     * 请求跨域
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //添加映射路径
        registry.addMapping("/**")
                //放行哪些原始域
                .allowedOrigins("*")
                //是否发送Cookie信息
                .allowCredentials(true)
                //放行哪些原始域(请求方式)
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                //放行哪些原始域(头部信息)
                .allowedHeaders("*")
                //暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
                .exposedHeaders("Header1", "Header2");
//        registry.addMapping("/api/v1/agency/**")
//                .allowedOrigins("*")
//                .allowCredentials(true)
//                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
//                .exposedHeaders("Header1", "Header2");
    }


}
